package com.hyny.framework.platform.system.entity.vo;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.hyny.framework.platform.system.entity.dto.LoginRoleFieldPowerDTO;
import com.hyny.framework.platform.system.entity.dto.LoginUserDataPowerDTO;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

//@Schema(title = "用户信息",description="登录用户信息")
@ApiModel
@Data
@NoArgsConstructor
@AllArgsConstructor
public class LoginVO implements UserDetails {
    /**
     * 用户信息
     */
    private LoginUserVO user;
    /**
     * 用户权限信息（数据库取出）
     */
    private List<String> permissions;
    /**
     * 用户角色（数据库取出）
     */
    private List<String> roleIds;
    /**
     * 数据权限
     */
    private LoginUserDataPowerDTO dataPowerDTOs;

    private List<LoginRoleFieldPowerDTO> fieldPowerDTOs;



    /**
     * 用户权限信息（SpringSecurity用）
     * 存储到Redis时不序列化该集合对象，否则会抛异常
     */
    @JSONField(serialize = false)
    private List<SimpleGrantedAuthority> authorities;

    public LoginVO(LoginUserVO user, List<String> permissions, List<String> roleIds, LoginUserDataPowerDTO dataPowerDTOs, List<LoginRoleFieldPowerDTO>  fieldPowerDTOs) {
        this.user = user;
        this.permissions = permissions;
        this.roleIds = roleIds;
        this.dataPowerDTOs = dataPowerDTOs;
        this.fieldPowerDTOs = fieldPowerDTOs;
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (ObjectUtil.isEmpty(authorities)) {
            authorities = permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        }
        return authorities;
    }

    @Override
    @JSONField(serialize = false)
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
//    /**
//     * 判断用户是否过期
//     * @return
//     */
//    @Override
//    public boolean isAccountNonExpired() {
//        if(new Date().compareTo(user.getExpiredDate())<0){
//            return true;
//        }
//        return false;
//    }
//
//    /**
//     * 判断用户是否绑定角色信息
//     * @return
//     */
//    @Override
//    public boolean isAccountNonLocked() {
//        if(roleIds!=null&&roleIds.size()>0){
//            return true;
//        }
//        return false;
//    }
//
//    /**
//     * 用户密码是否过期，暂不做支持
//     * @return
//     */
//    @Override
//    public boolean isCredentialsNonExpired() {
//        return true;
//    }
//
//
//    /**
//     * 判断用户是否可用
//     */
//    @Override
//    public boolean isEnabled() {
//        if(user.getIsEnabled()== YesOrNoEnum.NO){
//            return true;
//        }
//        return false;
//    }
}
